//
//  LMTableViewCell.m
//  舍品
//
//  Created by 楼 眉 on 16/2/22.
//  Copyright © 2016年 楼 眉. All rights reserved.
//

#import "LMTableViewCell.h"
#import "LMPostFrame.h"
#import <YYWebImage.h>
@interface LMTableViewCell ()
@property (nonatomic,strong)  UIImageView *topView;
@property (nonatomic,strong)  UIImageView *iconView;
@property (nonatomic,strong)  UILabel *nameLabel;
@property (nonatomic,strong)  UILabel *timeLabel;

@property (nonatomic,strong)  UIImageView *pictureView;
@property (nonatomic,strong)  UIImageView *bottomView;
@property (nonatomic,strong)  UILabel *postTitleLabel;
@property (nonatomic,weak)  UIButton *reportBtn;
@end
@implementation LMTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubViews];
    }
    return self;
}

#pragma mark - 初始化子控件
- (void)setupSubViews
{
    //上部View
    UIImageView *topView = [[UIImageView alloc]init];
    topView.userInteractionEnabled = YES;
    self.topView = topView;
    [self.contentView addSubview:topView];
    
    UIImageView *iconView = [[UIImageView alloc]init];
    iconView.userInteractionEnabled = YES;
    self.iconView = iconView;
    iconView.contentMode = UIViewContentModeScaleAspectFill;
    iconView.clipsToBounds = YES;
    [topView addSubview:iconView];
    
    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.textColor = [UIColor blackColor];
    nameLabel.font = [UIFont systemFontOfSize:11];
    nameLabel.numberOfLines = 0;
    self.nameLabel = nameLabel;
    [topView addSubview:nameLabel];
    
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.text = @"49分钟前";
    self.timeLabel = timeLabel;
    [topView addSubview:timeLabel];
    
    //中部View
    UIImageView *pictureView = [[UIImageView alloc]init];
    pictureView.userInteractionEnabled = YES;
    self.pictureView = pictureView;
    [self.contentView addSubview:pictureView];
    
    //帖子标题
    UILabel *postTitleLabel = [[UILabel alloc]init];
    self.postTitleLabel = postTitleLabel;
    [pictureView addSubview:postTitleLabel];
    
    //底部View
    UIImageView *bottomView = [[UIImageView alloc]init];
    self.bottomView = bottomView;
    bottomView.userInteractionEnabled = YES;
    [self.contentView addSubview:bottomView];
    
    //举报按钮
    UIButton *reportBtn = [[UIButton alloc]init];
    self.reportBtn = reportBtn;
    [bottomView addSubview:reportBtn];
    [reportBtn addTarget:self action:@selector(reportBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark - 举报按钮被点击
- (void)reportBtnClick
{
}
#pragma mark - postFrame set方法 设置时给各控件赋值
- (void)setPostFrame:(LMPostFrame *)postFrame
{
    _postFrame = postFrame;
    self.topView.frame = postFrame.topViewF;
    self.topView.backgroundColor = RGBCOLOR(240, 240, 240);
    
    self.iconView.frame = postFrame.iconViewF;
    [CALayer LM_RoundedImageWIthImageViewCALayer:self.iconView.layer Width:self.iconView.width];
    
    LMUser *user = postFrame.post.writer;
    [user fetchIfNeededInBackgroundWithBlock:^(AVObject *object, NSError *error) {
        self.nameLabel.text = user.nickname;
        NSURL *iconImageUrl = [NSURL URLWithString:user.iconImage.url];
        [self.iconView yy_setImageWithURL:iconImageUrl placeholder:nil];
        
    }];
    self.nameLabel.frame = postFrame.nameLabelF;
    self.nameLabel.textAlignment = NSTextAlignmentLeft;
    self.nameLabel.font = [UIFont systemFontOfSize:11];
    self.nameLabel.textColor = [UIColor purpleColor];
    
    NSString *timeString = [self calculateDate:postFrame.post.createdAt];
    self.timeLabel.text = timeString;
    self.timeLabel.frame = postFrame.timeLabelF;
    self.timeLabel.textAlignment = NSTextAlignmentRight;
    self.timeLabel.font  = [UIFont systemFontOfSize:11];
    self.timeLabel.textColor = [UIColor orangeColor];
    
    self.pictureView.image = [UIImage imageNamed:@"占位图.png"];
    [self.pictureView setContentMode:UIViewContentModeScaleAspectFill];
    self.pictureView.clipsToBounds = YES;
    NSArray *contents = [postFrame.post objectForKey:@"contentArray"];
    for (int index = 0; index < contents.count; index ++) {
        NSDictionary *dict = contents[index];
        NSString *keyString = [dict.allKeys firstObject];
        if ([keyString isEqualToString:@"image"]) {
            NSString *urlString = dict[@"image"];
            if (urlString) {
                [self.pictureView yy_setImageWithURL:[NSURL URLWithString:urlString] placeholder:[UIImage imageNamed:@"占位图.png"]];
            }else
            {
                self.pictureView.image = [UIImage imageNamed:@"占位图.png"];
            }

        }else
        {
        }
    }
    self.pictureView.frame = postFrame.pictureViewF;
//    NSURL *url = [NSURL URLWithString:postFrame.post.picture.url];
//    [self.pictureView yy_setImageWithURL:url placeholder:[UIImage imageNamed:@"占位图.png"]];
    self.pictureView.backgroundColor = RGBCOLOR(240, 240, 240);
    
    self.postTitleLabel.frame = postFrame.postTitleLabelF;
    _postTitleLabel.textColor = [UIColor whiteColor];
    _postTitleLabel.textAlignment = NSTextAlignmentCenter;
    _postTitleLabel.textAlignment = NSTextAlignmentCenter;
    _postTitleLabel.backgroundColor = [UIColor blackColor];
    _postTitleLabel.alpha = 0.5;
    _postTitleLabel.text = postFrame.post.title;

    self.bottomView.frame = postFrame.bottomViewF;
    self.bottomView.backgroundColor = RGBCOLOR(240, 240, 240);
    
    self.reportBtn.frame = postFrame.reportBtnF;
    [self.reportBtn setImage:[UIImage imageNamed:@"mainCellShare"] forState:UIControlStateNormal];
    [self.reportBtn setImage:[UIImage imageNamed:@"mainCellShareClick"] forState:UIControlStateHighlighted];
}
#pragma mark - 计算日期
- (NSString *)calculateDate:(NSDate *)PostCreatedAt
{
    NSDateFormatter *ft = [[NSDateFormatter alloc]init];
    NSString *timeString = nil;
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:PostCreatedAt toDate:[NSDate date] options:0];
    long year = [components year];
    long month = [components month];
    long day = [components day];
    if (year == 0 && month == 0 && day ==0) {
        ft.dateFormat = @"HH:mm";
        NSString *mmssString = [ft stringFromDate:PostCreatedAt];
        timeString = [NSString stringWithFormat:@"今天 %@",mmssString];
    }
    else if (year == 0)
    {
        ft.dateFormat = @"MM月dd日 HH:mm";
        timeString = [ft stringFromDate:PostCreatedAt];
    }
    else
    {
        ft.dateFormat = @"yyyy年MM月dd日 HH:mm";
        timeString = [ft stringFromDate:PostCreatedAt];
    }
    return timeString;
}
#pragma mark - 重新set frame方法 调整自己位置
- (void)setFrame:(CGRect)frame
{
    frame.origin.y += 10;
    frame.size.height -= 10;
    [super setFrame:frame];
}

#pragma mark - 重写setHighlighted方法并为空 取消高亮状态
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{}
@end
